/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

import com.core.log.Debug;
import com.core.utils.Utils;

/**
 *
 * @author daz
 */
public class TienlenTable extends GameTable {

    private static final int NUMBER_TIEN_LEN_PLAYERS = 4;
    private static final int NUMBER_CARDS = 52;
    private static final int NUMBER_CARD_SHARE = 13;
    private int[] cardIds;
    private int[] gameResult;
    private int numberPlayerFinish;
    private boolean[] isFinishId;
    private ShareCard dealer;
    private int playerWin;

    public TienlenTable(GameManager gameManager, GameRoom parent, long moneyBetDefault) {
        super(GameManager.TIEN_LEN, NUMBER_TIEN_LEN_PLAYERS, gameManager, parent, moneyBetDefault);
        cardIds = new int[NUMBER_CARDS];
        gameResult = new int[NUMBER_TIEN_LEN_PLAYERS];
        isFinishId = new boolean[NUMBER_TIEN_LEN_PLAYERS];
        dealer = new ShareCard();
        playerWin = -1;
    }

    private void prepareShare() {
        dealer.initCard();
        int count = 0;
        for (int i = 0; i < maxPlayers; i++) {
            if (listPlayer[i] != null) {
                if (listPlayer[i].getItemType() == 2) {
                    dealer.insertKing((count + 1) * 13 - 3);
                    count++;
                    continue;
                }
                if (listPlayer[i].getItemType() == 0) {
                    dealer.insertTwoCard((count + 1) * 13 - 2);
                    count++;
                    continue;
                }
                if (listPlayer[i].getItemType() == 1) {
                    dealer.insertAceCard((count + 1) * 13 - 1);
                    count++;
                    continue;
                }
            }
        }
        cardIds = dealer.getCardId();
    }

    @Override
    public void processMessage(String request, int playerId) {
        super.processMessage(request, playerId);
        String[] message = Utils.split(request, '|');
        if (message[0].equals("DB2") && playerId < maxPlayers) {
            if (playerId < maxPlayersInMatch) {
                listPlayer[playerId].setStartTimeDb(getNumberActivedPlayers());
            }
            sendToAll(request);
            return;
        }
        if (message[0].equals("BL")) {
            if (playerId < maxPlayersInMatch) {
                listPlayer[playerId].setStartTimeDb(getNumberActivedPlayers());
            }
            sendToAll(request);
            return;
        }
        if (message[0].equals("FN2")) {
            // gameresult/gamekind/playerid
            int playerSendId = Integer.parseInt(message[3]);
            int result = Integer.parseInt(message[1]);
            listPlayer[playerSendId].resetTimeDb();
            gameResult[playerSendId] = result;
            isFinishId[playerSendId] = true;
            if (result == 0) {
                playerWin = playerSendId;
            }
            sendToAll(request);
            numberPlayerFinish++;
            if (numberPlayerFinish == getNumberActivedPlayers() - 1) {
                sendMoneyChange();
            }
            return;
        }
        if (message[0].equals("FN1")) {
            int playerSendId = Integer.parseInt(message[1]);
            for (int i = 0; i < getNumberActivedPlayers(); i++) {
                gameResult[i] = (playerSendId == i ? 0 : 1);
                isFinishId[i] = true;
                if (listPlayer[i] != null) {
                    listPlayer[i].resetTimeDb();
                }
            }
            playerWin = playerSendId;
            numberPlayerFinish = getNumberActivedPlayers();
            sendToAll(request);
            sendMoneyChange();
            return;
        }
        if (message[0].equals("FN3")) {
            int playerSendId = Integer.parseInt(message[3]);
            int result = Integer.parseInt(message[1]);
            listPlayer[playerSendId].resetTimeDb();
            gameResult[playerSendId] = result;
            isFinishId[playerSendId] = true;
            if (result == 0) {
                playerWin = playerSendId;
            }
            StringBuilder buffer = new StringBuilder();
            buffer.append("FN2|");
            for (int i = 1, l = message.length; i < l; i++) {
                buffer.append(message[i]);
                if (i != l - 1) {
                    buffer.append("|");
                }
            }
            numberPlayerFinish++;
            sendToAll(buffer.toString());
            if (numberPlayerFinish == getNumberActivedPlayers() - 1) {
                sendMoneyChange();
            }
            return;
        }
        if (message[0].equals("TH")) {
            sendToAll(request);
            return;
        }
        if (message[0].equals("CT")) {
            resetTable();
            sendListPlayer();
            sendMoneyInfo();
            sendReadyState();
            if (listPlayer[playerId] != null) {
                listPlayer[playerId].resetTimeDb();
            }
            isPlay = false;
            return;
        }
    }

    private void setLastPlayerResult() {
        for (int i = 0; i < getNumberActivedPlayers(); i++) {
            if (!isFinishId[i] && listPlayer[i] != null && i < maxPlayersInMatch) {
                gameResult[i] = numberPlayerFinish;
                listPlayer[i].resetTimeDb();
                break;
            }
        }
    }

    private long getMoneyWin(int numberPlayer) {
        switch (numberPlayer) {
            case 2:
                return (tableMoneyBet * 9 / 10);
            case 3:
                return (tableMoneyBet * 3 * 9 / 10);
            case 4:
                return (tableMoneyBet * 6 * 9 / 10);

        }
        return 0;
    }

    public void sendMoneyChange() {
        setLastPlayerResult();
        StringBuilder[] moneyBuilders = new StringBuilder[getNumberActivedPlayers()];
        int arrIndex = 0;
//        int numberPlayerActive = getNumberActivedPlayers();

        for (int i = 0; i < maxPlayersInMatch; i++) {
            if (listPlayer[i] != null) {
                StringBuilder moneyInfoBuilder = new StringBuilder();
                moneyInfoBuilder.append("CM");
                if (gameResult[i] == 0) {
                    moneyInfoBuilder.append("|");
                    moneyInfoBuilder.append(i);
                    moneyInfoBuilder.append("|");
                    moneyInfoBuilder.append(getMoneyWin(getNumberActivedPlayers()));
                    listPlayer[i].updateMoneyDelta(getMoneyWin(getNumberActivedPlayers()));
                    listPlayer[i].updateWin();
                } else {
                    long moneyChange = 0;
                    if (getNumberActivedPlayers() <= 2) {
                        moneyChange = tableMoneyBet;
                    } else {
                        moneyChange = moneyChange = getMoneyChange(gameResult[i], tableMoneyBet);
                    }
                    moneyInfoBuilder.append("|");
                    moneyInfoBuilder.append(i);
                    moneyInfoBuilder.append("|");
                    moneyInfoBuilder.append(-moneyChange);
                    listPlayer[i].updateMoneyDelta(-moneyChange);
                    listPlayer[i].updateLose();
                }
                moneyBuilders[arrIndex] = moneyInfoBuilder;
                arrIndex++;
            }
        }
        for (int i = 0; i < arrIndex; i++) {
            sendToAll(moneyBuilders[i].toString());
        }
        sendToAll("FCM|");
        isPlay = false;
    }

    private long getMoneyChange(int result, long moneyBet) {
        switch (result) {
            case 1:
                return (moneyBet);
            case 2:
                return (moneyBet * 2);
            case 3:
                return (moneyBet * 3);
        }
        return 0;
    }

    private void shareCardToPlayer() {
        prepareShare();
        int startIndex = 0;
        theFirstTurn = playerWin;
        if (playerWin != -1 && theFirstTurn != -1) {
            if (listPlayer[playerWin] == null) {
                theFirstTurn = -1;
            }
        }
//        if (theFirstTurn == -1) {
//            for (int i = 0, l = NUMBER_CARD_SHARE * getNumberActivedPlayers(); i < l; i++) {
//                if (cardIds[i] == 2) {
//                    theFirstTurn = i;
//                    break;
//                }
//            }
//            theFirstTurn = theFirstTurn / NUMBER_CARD_SHARE;
//        }
        for (int i = 0; i < maxPlayersInMatch; i++) {
            if (listPlayer[i] != null) {
                StringBuilder buffer = new StringBuilder();
                buffer.append("SC|");
                for (int j = startIndex; j < startIndex + NUMBER_CARD_SHARE; j++) {
                    if (theFirstTurn == -1) {
                        if (cardIds[j] == 2) {
                            theFirstTurn = i;
                        }
                    }
                    buffer.append(cardIds[j]);
                    if (j != startIndex + NUMBER_CARD_SHARE - 1) {
                        buffer.append("|");
                    }
                }
                listPlayer[i].response(buffer.toString());
                startIndex += NUMBER_CARD_SHARE;
            }
        }
        if (theFirstTurn == -1) {
            for (int i = 0; i < maxPlayersInMatch; i++) {
                if (listPlayer[i] != null) {
                    theFirstTurn = i;
                    break;
                }
            }
        }
        sendToAll("FS|" + theFirstTurn);
    }

    @Override
    public void startPlay() {
        for (int i = 0; i < maxPlayersInMatch; i++) {
            if (listPlayer[i] != null) {
                listPlayer[i].setReadyState(0);
            }
        }
        resetFinishArr();
        shareCardToPlayer();
        //  betMoney();
    }

    private void resetFinishArr() {
        numberPlayerFinish = 0;
        for (int i = 0; i < getNumberActivedPlayers(); i++) {
            if (listPlayer[i] != null && i < maxPlayersInMatch) {
                isFinishId[i] = false;
            }
        }
    }

    @Override
    public void sendGameInfo() {
    }

    @Override
    protected void resetGame() {
        playerWin = -1;
    }
}
